﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace FileMonSync
{
    class AppHandler
    {
        public static string BACKUP_PREFIX = Path.Combine(Program.REPO_PREFIX,"backup");

        private string name;
        private string appDir;
        private string[] files;
        private FileSystemWatcher fsw;

        public AppHandler(string name,string appDir,string[] files)
        {
            this.name = name;
            this.appDir = appDir;
            this.files = files;
            backupAll(false);
            regWatcher();
            startWatcher();
        }

        private void regWatcher()
        {
            fsw = new FileSystemWatcher();
            fsw.Path = appDir;
            //fsw.Filter = String.Join("|",this.files);
            fsw.IncludeSubdirectories = true;
            fsw.NotifyFilter = NotifyFilters.LastWrite;// | NotifyFilters.Size;

            WatcherTimer watcher = new WatcherTimer(fsw, OnChanged);

        }

        private void startWatcher()
        {
            fsw.EnableRaisingEvents = true;
        }

        private void stopWatcher()
        {
            fsw.EnableRaisingEvents = false;
        }

        private void backupAll(bool force){
            if (Directory.Exists(this.appDir))
            {
                foreach (string file in this.files)
                {
                    string path = Path.Combine(BACKUP_PREFIX, Path.Combine(this.name, file));
                    if(force || !File.Exists(path)){
                        backup(file);
                    }
                }
            }
        }

        private void OnChanged(object source, FileSystemEventArgs e)
        {
            //Console.WriteLine("{0}\t", e.Name);
            FileInfo fi = new FileInfo(e.FullPath);
            if (!fi.Directory.Equals(Environment.CurrentDirectory) 
                && fi.Attributes != FileAttributes.Directory
                && this.files.Contains(e.Name))
            {
                backup(fi,e.Name);
                //FileInfo target = new FileInfo(Path.Combine(REPO_PREFIX,Path.Combine(this.name,e.Name)));
                //Directory.CreateDirectory(target.Directory.ToString());
                //fi.CopyTo(target.FullName, true);

                //Console.WriteLine("{0} copied!", e.Name);
            }

        }

        private void backup(string filename)
        {
            string srcFullPath = Path.Combine(this.appDir,filename);
            string repoFullPath = Path.Combine(BACKUP_PREFIX, Path.Combine(this.name, filename));
            copyTo(srcFullPath, repoFullPath);
        }

        private void backup(FileInfo src,string filename)
        {
            string repoFullPath = Path.Combine(BACKUP_PREFIX, Path.Combine(this.name, filename));
            copyTo(src, repoFullPath);
        }

        private void restore(string filename)
        {
            string srcFullPath = Path.Combine(this.appDir, filename);
            string repoFullPath = Path.Combine(BACKUP_PREFIX, Path.Combine(this.name, filename));
            copyTo(repoFullPath, srcFullPath);
        }

        private void copyTo(string src,string tgt)
        {
            FileInfo srcfi = new FileInfo(src);
            FileInfo tgtfi = new FileInfo(tgt);
            tgtfi.Directory.Create();
            srcfi.CopyTo(tgt, true);
        }

        private void copyTo(FileInfo src, string tgt)
        {
            FileInfo tgtfi = new FileInfo(tgt);
            tgtfi.Directory.Create();
            src.CopyTo(tgt, true);
        }
    }
}
